package com.zingrow.web.configure.weixin.service.impl;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import jxl.Sheet;
import jxl.Workbook;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zingrow.common.utils.CommonUtils;
import com.zingrow.common.utils.PageView;
import com.zingrow.web.base.service.impl.BaseService;
import com.zingrow.web.common.utils.AnalyzeResult;
import com.zingrow.web.configure.weixin.mapper.WeixinMapper;
import com.zingrow.web.configure.weixin.model.Weixin;
import com.zingrow.web.configure.weixin.service.IWeixinService;

@Service
public class WeixinService extends BaseService<Weixin> implements IWeixinService {
	@Autowired
	private WeixinMapper weixinMapper;
	@Override
	public void initMapper(){
		this.baseMapper = weixinMapper;
	}
	//模糊查询
	@Override
	public List<Weixin> queryByParams(PageView pageView,String weixinuser_Id,String weixin_name,Integer is_exprot){
		Map<String, Object> map = new HashMap<String, Object>();
		//添加分页要的参数
		map.put("paging", pageView);
		//添加查询要的参数
		map.put("weixinuser_Id", weixinuser_Id);
		map.put("weixin_name", weixin_name);
		map.put("is_exprot", is_exprot);
		
		return weixinMapper.queryByParams(map);
	}
	//新增
	@Override
	public boolean addWeixin(Weixin weixin){
		//给状态赋值为1
		if(weixin.getIs_delete()==null){
			weixin.setIs_delete(1);
		}
		//导出状态默认值为0
		if(weixin.getIs_exprot()==null){
			weixin.setIs_exprot(0);
		}
		return weixinMapper.addWeixin(weixin) > 0;
	}
	//修改
	@Override
	public boolean modifyWeixin(Weixin weixin){
		return weixinMapper.modifyWeixin(weixin) > 0;
	}
	//批量删除
	@Override
	public boolean batchDeletes(List delList ){
		return weixinMapper.batchDeletes(delList) > 0;
	}
	//删除
	@Override
	public boolean deleteWeixin(Integer weixin_id){
		return weixinMapper.deleteWeixin(weixin_id) > 0;
	}
	//查询相同微信id
	@Override
	public boolean queryById(Integer weixin_id,String weixinuser_Id){
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("weixin_id", weixin_id);
		map.put("weixinuser_Id", weixinuser_Id);
		//map.put("weixin_name", weixin_name);
		return weixinMapper.queryById(map) > 0;
	}
	//单个查询
	@Override
	public Weixin queryWeixinById(Integer weixin_id){
		Weixin weixin = weixinMapper.queryWeixinById(weixin_id);
		return weixin;
	}
	//查询全部的接口
	@Override
	public List<Weixin> queryByIdName(PageView pageView){
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("paging", pageView);
		return weixinMapper.queryByIdName(map);
	}
	//接口
	@Override
	public List<Weixin> queryWeixinByIdList(List<String> idlist) {
		if(idlist != null){
			return weixinMapper.queryWeixinByIdList(idlist);
		}
		return null;
	}
	//导出
	@Override
	public List<Weixin> queryWeixinbyid(List<Integer> ids){
		List<Weixin> wlist = new ArrayList<Weixin>();
		//根据id调用导出方法
		wlist = weixinMapper.queryWeixinbyid(ids);
		//根据微信id修改导出状态
			if(wlist.size()>0){
				weixinMapper.modifyWeixinIsexprot(ids);
			}
		//判断是否有值
		if(wlist != null && wlist.size() > 0){
			//迭代集合
			for (Weixin weixin : wlist) {
				//判断
				if(weixin.getIs_delete()==0){
					weixin.setIs_deletes("停用");
				}else if(weixin.getIs_delete()==1){
					weixin.setIs_deletes("启用");
				}else{
					weixin.setIs_deletes("未知");
				}
			}
		}
		return wlist;
	}
    @Override
    public List<Weixin> queryWeixinIdByType(PageView pageView,String weixin_name, String weixinuser_Id) {
        Map <String,Object> map = new HashMap<String,Object>();
        map.put("paging", pageView);
        map.put("weixin_name", weixin_name);
        map.put("weixinuser_Id", weixinuser_Id);
        return weixinMapper.queryWeixinIdByType(map);
    }
  //修改显示
  	@Override
  	public boolean modifyDisplay(int display,List<String> displaylist){
  		if(display==1)
  		{
  			return weixinMapper.updatadisplay(displaylist)>0;
  		}
  		else
  		{
  			return weixinMapper.updatadisplayno(displaylist)>0;
  		}
  		}
	//导入
	public void analyzeExcelData(File file,AnalyzeResult<Weixin> result)throws Exception{
		List<Weixin> list = new ArrayList<Weixin>();
		//解析文件
		Workbook work = Workbook.getWorkbook(file);
		//获取第一个工作表
		Sheet sheet = work.getSheet(0);
		int cols = 0;
		int rows = 0;
		//判断工作表获取是否正确
		if(CommonUtils.isObjectNotNull(sheet)){
			//得到列的数量
			cols = sheet.getColumns();
			rows = sheet.getRows();
			if(cols<=1||rows<=1){
				result.setResult(false, "未检测到数据", new ArrayList<Weixin>());
			}else if(rows>=999999){
				result.setResult(false, "导入数量过多，请拆分多次导入", new ArrayList<Weixin>());
			}
		}else{
			result.setResult(false, "Excel表格，格式错误", new ArrayList<Weixin>());
		}
		if(!result.isSuccess()){
			return;
		}
		//获取正常，进行下一个判断
		for (int i = 1; i < rows; i++) {
			for (int j = 0; j < cols; j++) {
				String weixinuser_Id = sheet.getCell(j++, i).getContents().trim();
				String weixin_name = sheet.getCell(j++, i).getContents().trim();
				
				//判断参数是否正确
				if (weixinuser_Id.length()<0||weixinuser_Id.length()>50) {
					result.setResult(false, "第"+(i+1)+"行，格式错误", new ArrayList<Weixin>());
				}
				if (StringUtils.isBlank(weixinuser_Id)) {
					result.setResult(false, "第"+(i+1)+"行，格式错误", new ArrayList<Weixin>());
				}
				if (StringUtils.isBlank(weixin_name)) {
					result.setResult(false, "第"+(i+1)+"行，格式错误", new ArrayList<Weixin>());
				}
				if (weixin_name.length()<0||weixinuser_Id.length()>50) {
					result.setResult(false, "第"+(i+1)+"行，格式错误", new ArrayList<Weixin>());
				}
				//以上判断通过，则添加数据
				if(result.isSuccess()){
					Weixin weixin = new Weixin(null, weixinuser_Id, weixin_name);
					list.add(weixin);
				}else{
					return;
				}
			}
		}
		result.setResult(true, null, list);
	}
}
